package com.camera2.frament;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.camera2.activity.R;

import java.util.ArrayList;
import java.util.List;

public class FramentRaise extends Fragment implements  OnPageChangeListener {

    private static final String TAG = "FramentDevice";
    private View view;
    private ListView lv;
    private SwipeRefreshLayout mSwipeLayout;


    private String empId;
    private LinearLayout llPoints;
    private int previousSelectPosition = 0;
    private ViewPager mViewPager;
    private List<ImageView> imageViewList;


    private boolean isLoop = true;
    private Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            mViewPager.setCurrentItem(mViewPager.getCurrentItem() + 1);
        }
    };

    @Override
    public View onCreateView(android.view.LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.frament_raise, null);
        view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

        mViewPager = view.findViewById(R.id.user_viewpager);
        llPoints = view.findViewById(R.id.ll_user_points);
        initView();
        return view;

    }




    public void initView() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (isLoop) {
                    SystemClock.sleep(3 * 1000);
                    handler.sendEmptyMessage(0);
                }
            }
        }).start();

        prepareData();
        ViewPagerAdapter adapter = new ViewPagerAdapter();
        mViewPager.setAdapter(adapter);

        mViewPager.setOnPageChangeListener(this);
        llPoints.getChildAt(previousSelectPosition).setEnabled(true);
        int n = Integer.MAX_VALUE / 2 % imageViewList.size();
        int itemPosition = Integer.MAX_VALUE / 2 - n;
        mViewPager.setCurrentItem(itemPosition);
    }

    private void prepareData() {
        imageViewList = new ArrayList<ImageView>();
        int[] imageResIDs = getImageResIDs();
        ImageView iv;
        View view;
        for (int i = 0; i < imageResIDs.length; i++) {
            iv = new ImageView(getActivity());
            iv.setBackgroundResource(imageResIDs[i]);
            imageViewList.add(iv);
            view = new View(getActivity());

            view.setBackgroundDrawable(getResources().getDrawable(
                    R.drawable.point_background));
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(3, 3);
            lp.leftMargin = 20;
            lp.height = 20;
            lp.width = 20;
            view.setLayoutParams(lp);
            view.setEnabled(false);
            llPoints.addView(view);
        }
    }

    private int[] getImageResIDs() {
        return new int[]{R.drawable.guanggao1, R.drawable.guanggao2,
                R.drawable.guanggao3,};
    }

    class ViewPagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return Integer.MAX_VALUE;
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(imageViewList.get(position
                    % imageViewList.size()));
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            try {
                container.addView(imageViewList.get(position
                        % imageViewList.size()));
                return imageViewList.get(position % imageViewList.size());
            } catch (Exception e) {
                e.printStackTrace();
                return true;
            }
        }
    }

    @Override
    public void onPageScrollStateChanged(int arg0) {
    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
    }

    @Override
    public void onPageSelected(int position) {
        llPoints.getChildAt(previousSelectPosition).setEnabled(false);
        llPoints.getChildAt(position % imageViewList.size()).setEnabled(true);
        previousSelectPosition = position % imageViewList.size();
    }
}
